<?php

/**
 * Class PersonalController
 * 个人主页控制器，实现用户基本个人信息业务处理
 */

class BaseInfoController extends \BaseController
{

    /**
     * 获取用户个人基本信息
     * 返回json格式:
     * ```
     * {
     *   "username":
     *   "email":
     *   "realname":
     *   "sex":
     *   "birthday"：
     *   "nickname":
     * }
     *```
     * @return Response
     */
    public function getBaseInfo()
    {
        $columns = ['username', 'email', 'realname', 'sex', 'birthday', 'nickname'];
        $info = User::getInfo(Auth::user()['id'], $columns);
        return Response::json($info[0]);
    }

    /**
     * 更新个人基本信息
     * 请求json格式:
     * ```
     * {
     *   "realname":
     *   "sex":
     *   "birthday":
     *   "nickname":
     * }
     * ```
     * @return Response
     */
    public function putBaseInfo()
    {
        $putData = Input::all();

        $retains = ['realname', 'nickname', 'birthday', 'sex'];
        $filteredData = $this->postFilter($putData, $retains);

        $validator = $this->getValidatorCheckWith($filteredData);

        if ($validator->fails()) {
            return parent::failResponse($validator->messages());
        } else {
            User::findOrFail(Auth::user()['id'])->update($filteredData);
            return parent::successResponse();
        }

    }

    protected function getValidatorCheckWith($filteredData)
    {
        return Validator::make($filteredData, $this->getRules(), $this->getValidateMsg());
    }

    protected function getRules()
    {
        return [
            'realname' => 'max:50',
            'nickname' => 'max:50',
            'birthday' => 'date_format:Y-m-d'
        ];
    }

    protected function getValidateMsg()
    {
        return [
            'max' => '该选项不能超过50字符',
            'date_format' => '非法日期格式'
        ];
    }



}
